package scuba.DiveBuddy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import org.joda.time.DateTime;
import scuba.DiveBuddy.Class.*;
import scuba.DiveBuddy.Core.Toolbelt;

import java.util.Calendar;

/**
 * @author Alex Anderson
 * @version 2
 * @since 19/01/14.
 *
 * Containing static methods for accessing and manipulating the UserProfileTable
 */
public class LoaderActivity extends Activity {


    //Constructor
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profileloader);

        DatabaseLoadingThread lt = new DatabaseLoadingThread(getApplicationContext());
        lt.start();
        try {
            lt.join();

            if(DiverProfile.getActiveProfile() == null)
                displayCreateProfile();
            else
                invokeHomepage();
        }
        catch(InterruptedException ie) {

        }
    }

    //<editor-fold desc="// -------- Actionbar Options Menu">
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.profilecreate_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.action_create:
                registerAndCreateProfile();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    //</editor-fold>

    public void displayCreateProfile() {

        //String version = getPackageManager().getPackageArchiveInfo(getPackageName(), 0).versionName;
        //todo: temp code delete
        DiverProfile p = DiverProfile.createAndRegisterUser("DemoBuild:027", "DiveBuddy", Toolbelt.Toolkit.getToday());

        Toast t = Toast.makeText(getApplicationContext(), "Profile Created", Toast.LENGTH_SHORT);
        t.show();
        invokeHomepage();

        //registerAndCreateProfile();
        //setTitle(R.string.activity_creating_header);
        //No profile has been created
        //load the creator interface and register a callback for the button
        //setContentView(R.layout.activity_profilecreator);
    }
    public void invokeHomepage() {
        Log.w("INTERFACE", "[LoaderActivity] Heading Home");
        Intent i = new Intent(this, HomepageActivity.class);
        startActivity(i);
        finish(); //should terminate this activity from the stack, meaning back cant get to me.
    }
    public void registerAndCreateProfile() {
        String forename, surname;

        try {
            forename = ((TextView)findViewById(R.id.diverForename)).getText().toString();
            surname = ((TextView)findViewById(R.id.diverSurname)).getText().toString();
        }
        catch(NullPointerException n) {
            //name was null
            forename = "FebruaryMock";
            surname = "Demo";
        }

        /* todo: profile rejection, name and DoB needs to be set.*/

        DatePicker dp = (DatePicker)findViewById(R.id.profileDOB);

        Calendar dob = Calendar.getInstance();
        dob.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth());

        DiverProfile p = DiverProfile.createAndRegisterUser(forename, surname, new DateTime(dob));
        if( p== null)
        {
            Toast t = Toast.makeText(getApplicationContext(), "An error occurred while creating profile, sorry!",
                    Toast.LENGTH_SHORT);
            t.show();
        }
        else
        {
            Toast t = Toast.makeText(getApplicationContext(), "Profile Created", Toast.LENGTH_SHORT);
            t.show();
            invokeHomepage();
        }

    }
}